package com.example.job_backend.controller;

import com.example.job_backend.model.Notice;
import com.example.job_backend.model.Query.NotificationQuery;
import com.example.job_backend.model.R;
import com.example.job_backend.service.NotificationService;
import com.example.job_backend.util.AliOSSUtils;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class NotificationController {
    @Resource
    private NotificationService notificationService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    //微信小程序用搜索
    @GetMapping("/notificationWx")
    public R getNoticeWx() {
        List<Notice> list = notificationService.list();
        return new R(200, "请求成功", list);
    }

    //管理系统用
    @GetMapping("/notifications")
    public R getNotice(NotificationQuery query) {
        PageInfo<Notice> list = notificationService.getNoticeByPage(query);
        return new R(200, "请求成功", list);
    }

    @GetMapping("/notification")
    public R getNoticeDetail(Integer id) {
        Notice notice = notificationService.getById(id);
        return new R(200, "请求成功", notice);
    }

    @PostMapping("/addNotice")
    public R addNotice(@RequestBody Notice notice) {
        notificationService.save(notice);
        return new R(200, "添加成功", null);
    }

    @PostMapping("/updateNotice")
    public R updateNotice(@RequestBody Notice notice) {
        notificationService.updateById(notice);
        return new R(200, "修改成功", null);
    }

    @GetMapping("/deleteNotice/{id}")
    public R deleteNotice(@PathVariable("id") Integer id) {
        notificationService.removeById(id);
        return new R(200, "删除成功", null);
    }

    @PostMapping("/uploadNoticeFile")
    public R uploadNoticeFile(MultipartFile file) throws IOException {
        String url= aliOSSUtils.uploadFile(file);
        return new R(200,"请求成功",url);
    }
}
